class Solution(object):
    def validWordAbbreviation(self, word, abbr):
        """
        :type word: str
        :type abbr: str
        :rtype: bool
        """
        n1 = len(word)
        n2 = len(abbr)
        num = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
        w_index = 0
        left_index = 0
        right_index = 0
        state = 0
        while w_index < n1 and right_index < n2:
            if not state:
                if abbr[right_index] not in num:
                    if word[w_index] != abbr[right_index]:
                        return False
                    right_index += 1
                    w_index += 1
                else:
                    left_index = right_index
                    right_index += 1
                    state = 1
            else:
                if abbr[right_index] not in num:
                    num_s = abbr[left_index: right_index]
                    if num_s[0] == '0':
                        return False
                    else:
                        t = int(num_s)
                    w_index = w_index + t
                    if w_index >= n1:
                        return False
                    if word[w_index] != abbr[right_index]:
                        return False
                    right_index += 1
                    w_index += 1
                    state = 0
                else:
                    right_index += 1
        if state == 1:
            num_s = abbr[left_index: right_index]
            if num_s[0] == '0':
                return False
            else:
                t = int(num_s)
            w_index = w_index + t
            if w_index != n1:
                return False
        if not (w_index == n1 and right_index == n2):
            return False
        return True


data = Solution()
word = "apple"
abbr = "a2le"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "s10n"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "sub4u4"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "12"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "su3i1u2on"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "substitution"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "s55n"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "s010n"
print(data.validWordAbbreviation(word, abbr))
word = "substitution"
abbr = "s0ubstitution"
print(data.validWordAbbreviation(word, abbr))
word = "internationalization"
abbr = "i12iz4n"
print(data.validWordAbbreviation(word, abbr))
word = "hit"
abbr = "h3"
print(data.validWordAbbreviation(word, abbr))